import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class SettingsScreen extends StatefulWidget {
  @override
  _SettingsScreenState createState() => _SettingsScreenState();
}

class _SettingsScreenState extends State<SettingsScreen> {
  late final SharedPreferences _prefs;
  bool _isDarkModeEnabled = false;

  String? _resDir;

  void _handleDarkModeSwitch(bool value) {
    setState(() {
      _isDarkModeEnabled = value;
    });
  }

  Future<void> _chooseResDir() async {
    String? selectedDirectory = await FilePicker.platform.getDirectoryPath();
    if (selectedDirectory != null) {
      print("设置库存目录为$selectedDirectory");
      await _prefs.setString("dir", selectedDirectory);
      setState(() {
        _resDir = selectedDirectory;
      });
    }
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    loadSP();
  }

  Future<void> loadSP() async {
      _prefs = await SharedPreferences.getInstance();
    setState(() {
      _resDir = _prefs.getString("dir")!;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Settings'),
      ),
      body: ListView(
        padding: EdgeInsets.all(16.0),
        children: <Widget>[
          ListTile(
            title: Text('资源库路径'),
            subtitle: Text(_resDir == null ? "未设置" : _resDir!),
            onTap: _chooseResDir,
          ),
          SwitchListTile(
              value: _isDarkModeEnabled,
              title: Text('Dark Mode'),
              onChanged: _handleDarkModeSwitch),
          const AboutListTile(
            // icon: Image.asset('images/app_icon.png'), // 使用应用的图标
            child: Text('关于这款应用'), // 关于的标题
            applicationName: 'My App', // 应用名称
            applicationVersion: '1.0.0', // 应用版本号
            applicationLegalese: '© 2023 My App Inc.', // 法律信息
          ),
          // Add more settings here...
        ],
      ),
    );
  }
}
